package com.bjesc_b2c.app.appraiset.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.bjesc_b2c.app.appraiset.R;
import com.example.codeutils.utils.SizeUtils;

public class IndexView extends View {
	private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private Paint mPaint;

	public IndexView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	public IndexView(Context context) {
		super(context, null);
		// TODO Auto-generated constructor stub
	}
	private void init() {
		//初始化画笔设置
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(SizeUtils.dp2px(14));
	}
	private int width, height, singleHeight;
	float baseline;
	private int currentTouchIndex = -1;

	@Override
	protected void onDraw(Canvas canvas) {
		//获取控件宽高并设置基准线
		if (width == 0) {
			width = getWidth();
			height = getHeight();
			FontMetrics metrics = mPaint.getFontMetrics();
			singleHeight = height / LETTERS.length();
			RectF f = new RectF(0, 0, width, singleHeight);
			baseline = (f.bottom + f.top - metrics.top - metrics.bottom) / 2;
		}
		//绘制字母
		for (int i = 0; i < LETTERS.length(); i++) {
			String text = LETTERS.charAt(i) + "";
			float x = (width - mPaint.measureText(text)) / 2;
			if (i == currentTouchIndex) {
				mPaint.setColor(getContext().getResources().getColor(R.color.colorFFCF021A));
			} else {
				mPaint.setColor(getContext().getResources().getColor(R.color.colorFFFF7900));
			}
			canvas.drawText(text, x, baseline + i * singleHeight, mPaint);
		}
		super.onDraw(canvas);
	}

	//由ListView点击Item后返回的首字母，用户定位到用户选中的字母
	public void refresh(char c) {
		for (int i = 0; i < LETTERS.length(); i++) {
			if (c == LETTERS.charAt(i)) {
				if (currentTouchIndex != i) {
					currentTouchIndex = i;
				}
				invalidate();
				return;
			}
		}
		invalidate();
	}

	//滑动索引控件并回调给ListView处理
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int index = (int) (event.getY() / singleHeight);
		if (index < 0 || index > LETTERS.length() - 1) {
			return true;
		}
		if (index != currentTouchIndex) {
			currentTouchIndex = index;
			invalidate();
			if (indexListenre != null) {
				indexListenre.setIndexListenre(LETTERS.charAt(index));
			}
		}
		return true;
	}

	public interface OnSetIndexListenre {
		void setIndexListenre(char index);
	}

	private OnSetIndexListenre indexListenre;

	public void setOnSetIndexListenre(OnSetIndexListenre indexListenre) {
		this.indexListenre = indexListenre;
	}
}
